/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.entities.AnimalSpecies;
import times.persistence.entities.PriceSlaughterLamb;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import times.persistence.controllers.exceptions.IllegalOrphanException;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.Category;
import times.persistence.entities.CategoryClass;

/**
 *
 * @author Administrator
 */
public class CategoryJpaController implements Serializable {

    public CategoryJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Category category) {
        if (category.getPriceSlaughterLambList() == null) {
            category.setPriceSlaughterLambList(new ArrayList<PriceSlaughterLamb>());
        }
        if (category.getCategoryClassList() == null) {
            category.setCategoryClassList(new ArrayList<CategoryClass>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            AnimalSpecies animalSpeciesId = category.getAnimalSpeciesId();
            if (animalSpeciesId != null) {
                animalSpeciesId = em.getReference(animalSpeciesId.getClass(), animalSpeciesId.getId());
                category.setAnimalSpeciesId(animalSpeciesId);
            }
            List<PriceSlaughterLamb> attachedPriceSlaughterLambList = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLambToAttach : category.getPriceSlaughterLambList()) {
                priceSlaughterLambListPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambList.add(priceSlaughterLambListPriceSlaughterLambToAttach);
            }
            category.setPriceSlaughterLambList(attachedPriceSlaughterLambList);
            List<CategoryClass> attachedCategoryClassList = new ArrayList<CategoryClass>();
            for (CategoryClass categoryClassListCategoryClassToAttach : category.getCategoryClassList()) {
                categoryClassListCategoryClassToAttach = em.getReference(categoryClassListCategoryClassToAttach.getClass(), categoryClassListCategoryClassToAttach.getId());
                attachedCategoryClassList.add(categoryClassListCategoryClassToAttach);
            }
            category.setCategoryClassList(attachedCategoryClassList);
            em.persist(category);
            if (animalSpeciesId != null) {
                animalSpeciesId.getCategoryList().add(category);
                animalSpeciesId = em.merge(animalSpeciesId);
            }
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLamb : category.getPriceSlaughterLambList()) {
                Category oldCategoryIdOfPriceSlaughterLambListPriceSlaughterLamb = priceSlaughterLambListPriceSlaughterLamb.getCategoryId();
                priceSlaughterLambListPriceSlaughterLamb.setCategoryId(category);
                priceSlaughterLambListPriceSlaughterLamb = em.merge(priceSlaughterLambListPriceSlaughterLamb);
                if (oldCategoryIdOfPriceSlaughterLambListPriceSlaughterLamb != null) {
                    oldCategoryIdOfPriceSlaughterLambListPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListPriceSlaughterLamb);
                    oldCategoryIdOfPriceSlaughterLambListPriceSlaughterLamb = em.merge(oldCategoryIdOfPriceSlaughterLambListPriceSlaughterLamb);
                }
            }
            for (CategoryClass categoryClassListCategoryClass : category.getCategoryClassList()) {
                Category oldCategoryIdOfCategoryClassListCategoryClass = categoryClassListCategoryClass.getCategoryId();
                categoryClassListCategoryClass.setCategoryId(category);
                categoryClassListCategoryClass = em.merge(categoryClassListCategoryClass);
                if (oldCategoryIdOfCategoryClassListCategoryClass != null) {
                    oldCategoryIdOfCategoryClassListCategoryClass.getCategoryClassList().remove(categoryClassListCategoryClass);
                    oldCategoryIdOfCategoryClassListCategoryClass = em.merge(oldCategoryIdOfCategoryClassListCategoryClass);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Category category) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Category persistentCategory = em.find(Category.class, category.getId());
            AnimalSpecies animalSpeciesIdOld = persistentCategory.getAnimalSpeciesId();
            AnimalSpecies animalSpeciesIdNew = category.getAnimalSpeciesId();
            List<PriceSlaughterLamb> priceSlaughterLambListOld = persistentCategory.getPriceSlaughterLambList();
            List<PriceSlaughterLamb> priceSlaughterLambListNew = category.getPriceSlaughterLambList();
            List<CategoryClass> categoryClassListOld = persistentCategory.getCategoryClassList();
            List<CategoryClass> categoryClassListNew = category.getCategoryClassList();
            List<String> illegalOrphanMessages = null;
            for (PriceSlaughterLamb priceSlaughterLambListOldPriceSlaughterLamb : priceSlaughterLambListOld) {
                if (!priceSlaughterLambListNew.contains(priceSlaughterLambListOldPriceSlaughterLamb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PriceSlaughterLamb " + priceSlaughterLambListOldPriceSlaughterLamb + " since its categoryId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (animalSpeciesIdNew != null) {
                animalSpeciesIdNew = em.getReference(animalSpeciesIdNew.getClass(), animalSpeciesIdNew.getId());
                category.setAnimalSpeciesId(animalSpeciesIdNew);
            }
            List<PriceSlaughterLamb> attachedPriceSlaughterLambListNew = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLambToAttach : priceSlaughterLambListNew) {
                priceSlaughterLambListNewPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListNewPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListNewPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambListNew.add(priceSlaughterLambListNewPriceSlaughterLambToAttach);
            }
            priceSlaughterLambListNew = attachedPriceSlaughterLambListNew;
            category.setPriceSlaughterLambList(priceSlaughterLambListNew);
            List<CategoryClass> attachedCategoryClassListNew = new ArrayList<CategoryClass>();
            for (CategoryClass categoryClassListNewCategoryClassToAttach : categoryClassListNew) {
                categoryClassListNewCategoryClassToAttach = em.getReference(categoryClassListNewCategoryClassToAttach.getClass(), categoryClassListNewCategoryClassToAttach.getId());
                attachedCategoryClassListNew.add(categoryClassListNewCategoryClassToAttach);
            }
            categoryClassListNew = attachedCategoryClassListNew;
            category.setCategoryClassList(categoryClassListNew);
            category = em.merge(category);
            if (animalSpeciesIdOld != null && !animalSpeciesIdOld.equals(animalSpeciesIdNew)) {
                animalSpeciesIdOld.getCategoryList().remove(category);
                animalSpeciesIdOld = em.merge(animalSpeciesIdOld);
            }
            if (animalSpeciesIdNew != null && !animalSpeciesIdNew.equals(animalSpeciesIdOld)) {
                animalSpeciesIdNew.getCategoryList().add(category);
                animalSpeciesIdNew = em.merge(animalSpeciesIdNew);
            }
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLamb : priceSlaughterLambListNew) {
                if (!priceSlaughterLambListOld.contains(priceSlaughterLambListNewPriceSlaughterLamb)) {
                    Category oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb = priceSlaughterLambListNewPriceSlaughterLamb.getCategoryId();
                    priceSlaughterLambListNewPriceSlaughterLamb.setCategoryId(category);
                    priceSlaughterLambListNewPriceSlaughterLamb = em.merge(priceSlaughterLambListNewPriceSlaughterLamb);
                    if (oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb != null && !oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb.equals(category)) {
                        oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListNewPriceSlaughterLamb);
                        oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb = em.merge(oldCategoryIdOfPriceSlaughterLambListNewPriceSlaughterLamb);
                    }
                }
            }
            for (CategoryClass categoryClassListOldCategoryClass : categoryClassListOld) {
                if (!categoryClassListNew.contains(categoryClassListOldCategoryClass)) {
                    categoryClassListOldCategoryClass.setCategoryId(null);
                    categoryClassListOldCategoryClass = em.merge(categoryClassListOldCategoryClass);
                }
            }
            for (CategoryClass categoryClassListNewCategoryClass : categoryClassListNew) {
                if (!categoryClassListOld.contains(categoryClassListNewCategoryClass)) {
                    Category oldCategoryIdOfCategoryClassListNewCategoryClass = categoryClassListNewCategoryClass.getCategoryId();
                    categoryClassListNewCategoryClass.setCategoryId(category);
                    categoryClassListNewCategoryClass = em.merge(categoryClassListNewCategoryClass);
                    if (oldCategoryIdOfCategoryClassListNewCategoryClass != null && !oldCategoryIdOfCategoryClassListNewCategoryClass.equals(category)) {
                        oldCategoryIdOfCategoryClassListNewCategoryClass.getCategoryClassList().remove(categoryClassListNewCategoryClass);
                        oldCategoryIdOfCategoryClassListNewCategoryClass = em.merge(oldCategoryIdOfCategoryClassListNewCategoryClass);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = category.getId();
                if (findCategory(id) == null) {
                    throw new NonexistentEntityException("The category with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Category category;
            try {
                category = em.getReference(Category.class, id);
                category.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The category with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<PriceSlaughterLamb> priceSlaughterLambListOrphanCheck = category.getPriceSlaughterLambList();
            for (PriceSlaughterLamb priceSlaughterLambListOrphanCheckPriceSlaughterLamb : priceSlaughterLambListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Category (" + category + ") cannot be destroyed since the PriceSlaughterLamb " + priceSlaughterLambListOrphanCheckPriceSlaughterLamb + " in its priceSlaughterLambList field has a non-nullable categoryId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            AnimalSpecies animalSpeciesId = category.getAnimalSpeciesId();
            if (animalSpeciesId != null) {
                animalSpeciesId.getCategoryList().remove(category);
                animalSpeciesId = em.merge(animalSpeciesId);
            }
            List<CategoryClass> categoryClassList = category.getCategoryClassList();
            for (CategoryClass categoryClassListCategoryClass : categoryClassList) {
                categoryClassListCategoryClass.setCategoryId(null);
                categoryClassListCategoryClass = em.merge(categoryClassListCategoryClass);
            }
            em.remove(category);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Category> findCategoryEntities() {
        return findCategoryEntities(true, -1, -1);
    }

    public List<Category> findCategoryEntities(int maxResults, int firstResult) {
        return findCategoryEntities(false, maxResults, firstResult);
    }

    private List<Category> findCategoryEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Category.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Category findCategory(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Category.class, id);
        } finally {
            em.close();
        }
    }

    public int getCategoryCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Category> rt = cq.from(Category.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
